package com.aces2win.server.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.jdo.PersistenceManager;

import com.aces2win.server.PMF;
import com.aces2win.server.entities.Quote;


public class YahooFinanceQuoteUtil {
	public static ArrayList<Quote> fetchAllQuotes(String symbol,Date from, Date to){
		try {
			GregorianCalendar calendar = new GregorianCalendar();
			calendar.setTime(from);
			String monthFrom = (new Integer(calendar.get(GregorianCalendar.MONTH))).toString();
			String dayFrom = (new Integer(calendar.get(GregorianCalendar.DAY_OF_MONTH))).toString();
			String yearFrom = (new Integer(calendar.get(GregorianCalendar.YEAR))).toString();
			calendar.setTime(to);
			String monthTo = (new Integer(calendar.get(GregorianCalendar.MONTH))).toString();
			String dayTo = (new Integer(calendar.get(GregorianCalendar.DAY_OF_MONTH))).toString();
			String yearTo = (new Integer(calendar.get(GregorianCalendar.YEAR))).toString();
			
			
            URL url = new URL("http://ichart.finance.yahoo.com/table.csv?s="+symbol+"&a="+
            monthFrom+"&b="+dayFrom+"&c="+yearFrom+"&d="+monthTo+"&e="+dayTo+"&f="+yearTo+"&g=d&ignore=.csv");
            
            BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
            String line;
            
            ArrayList<Quote> result=new ArrayList<Quote>();
            
            // 1st line is header
            reader.readLine(); 
            
            // parse file
            while ((line = reader.readLine()) != null) {
                String[] values = line.split(",");
                String date = values[0];
                Date dateQuote = new SimpleDateFormat("yyyy-MM-dd").parse(date);
                double open = Double.valueOf(values[1]);
                double high = Double.valueOf(values[2]);
                double low = Double.valueOf(values[3]);
                double close = Double.valueOf(values[4]);
                long volume = Long.valueOf(values[5]);
                double adjClose = Double.valueOf(values[6]);
                Quote q = new Quote(dateQuote,open,high,low,close,volume,adjClose);
                result.add(q);  
            }
            reader.close();
            Collections.reverse(result);
            return result;

        } catch (MalformedURLException e) {
            System.out.println("URL malformee");
        } catch (IOException e) {
        	System.out.println("Donnees illisibles");
        } catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public void saveQuotes(ArrayList<Quote> quotes){

		PersistenceManager pm = PMF.get().getPersistenceManager();
		pm.makePersistent(quotes);
		
	}
}
